﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ArduinoSharp.Lib
{
    public partial class ServoUserControl : UserControl
    {
        public ServoUserControl()
        {
            InitializeComponent();
        }
        protected float angle = 0.0f;
        public float Value
        {
            get
            {
                return angle;
            }
            set
            {
                // Maintain the Value between 0 and 100
                if (value < -180) value = -180;
                else if (value > 180) value = 180;
                angle = value;
                label1.Text = value.ToString() + "°";
                // Redraw the Pbar every time the Value changes
                this.Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServoUserControl));

            Bitmap servoMoverBMP = new Bitmap(global::ArduinoSharp.Lib.Properties.Resources.servo2);
            servoMoverBMP.MakeTransparent(Color.White);
            Graphics g = Graphics.FromImage(servoMoverBMP);
            g.TranslateTransform(50.0f, 100.0f);
            g.RotateTransform(this.Value);

            e.Graphics.DrawImage(servoMoverBMP, 0, 0, this.Width / 3, this.Height / 2);

            //Brush b = new SolidBrush(this.ForeColor); // Create a brush that will draw the background of the Pbar
            // Create a linear gradient that will be drawn over the background. FromArgb means you can use the Alpha value wich is the transparency
            //LinearGradientBrush lb = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), Color.FromArgb(255, Color.White),

            //Color.FromArgb(50, Color.White), LinearGradientMode.ForwardDiagonal);
            // Calculate how much has the Pbar to be filled for "x" %

            //int width = (int)((angle / 100) * this.Width);
            //e.Graphics.FillRectangle(b, 0, 0, width, this.Height);
            //e.Graphics.FillRectangle(lb, 0, 0, width, this.Height);
            //b.Dispose(); lb.Dispose();
        }

        /////////// Pbar_SizeChanged /////////////
        // Maintain the label in the center of the Pbar
        //label1.Location = new Point(this.Width / 2 - 21 / 2 - 4, this.Height / 2 - 15 / 2);
    }
}

